package com.zego.wawaji.ui.Fragment.wawa;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zego.wawaji.R;
import com.zego.wawaji.ui.Fragment.BaseFragment;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.base.smarttab.PagerItem;
import com.zego.wawaji.ui.base.smarttab.SmartTabLayout;
import com.zego.wawaji.ui.base.viewpager.ViewGroupPagerAdapter;
import com.zego.wawaji.ui.center.wawa.MyWawaSend;
import com.zego.wawaji.ui.center.wawa.MyWawaUntreated;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的娃娃
 * Created by Su on 2018/1/18.
 */
public class WawaFragment extends BaseFragment implements View.OnClickListener {
    private SmartTabLayout stlTitles;
    private ViewPager vpViewChange;
    private List<PagerItem> listViews;//pagerItem集合
    private List<BasePanel> panls = new ArrayList<>(); // Tab页面列表

    private MyWawaUntreated myWawaUntreated;
    private MyWawaSend myWawaSend;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.my_wawa_act);
        initView();
        return getContentView();
    }

    private void initView() {
        setTitle("我的娃娃");
        setTitleRight("地址管理", this);

        stlTitles = (SmartTabLayout) findViewById(R.id.stl_titles);
        vpViewChange = (ViewPager) findViewById(R.id.viewpager);
        initViewsList();
        initViewPager();
        ((LinearLayout) stlTitles.getTabStrip()).setGravity(Gravity.CENTER_HORIZONTAL);//标题居中
        stlTitles.setCustomTabView(R.layout.custom_table_view, R.id.tv_left_tab);//设置自定义标题
        stlTitles.setViewPager(vpViewChange);//设置viewpager
        stlTitles.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    /**
     * 添加展示panel
     */
    private void initViewsList() {
        listViews = new ArrayList<>();
        myWawaUntreated = new MyWawaUntreated(getActivity());
        myWawaSend = new MyWawaSend(getActivity());

        panls.add(myWawaUntreated);
        panls.add(myWawaSend);
        listViews.add(new PagerItem("寄存中", panls.get(0).getContentView()));
        listViews.add(new PagerItem("已申请发货", panls.get(1).getContentView()));
    }

    private void initViewPager() {
        vpViewChange.setAdapter(new ViewGroupPagerAdapter(listViews));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_right:
                UIShow.showMyAddressAct(getActivity());
                break;
        }
    }

    public void release() {
        if (myWawaUntreated != null)
            myWawaUntreated.release();
        if (myWawaSend != null)
            myWawaSend.release();
    }
}